var api = require('./../../../config/api.js');
var app = getApp();
var util = require('./../../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    address:[],
    orderType: '',
    toDo: 'express',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({ orderType: options.orderType });
    if (options.do){
      this.setData({ toDo: options.do });
    }
    this.loadList();
  },

  loadList: function(){
    var that = this;
    // 获取地址列表，为空则跳转到空页面
    var url = api.ReceiveAddressList + '/' + app.globalData.userInfo.token + '/' + that.data.orderType;
    util.myRequest(url, false, function(data){
      if (data.data.length == 0) {
        // 如果为空则跳转空页面
        wx.redirectTo({
          url: './../null/null?orderType=' + that.data.orderType + '&do=' + that.data.toDo
        })
        return;
      }

      // 加载地址列表
      that.setData({ address: that.listDataFormar(data.data) });
    });
  },

  listDataFormar: function(listData){
    var newList = [];
    listData.map(function (data, index) {
      console.log('map遍历:' + data + '--' + index);
      var newData = {};
      newData.id = data.aaId;
      newData.shcool = data.csName;
      newData.site = data.ssName;
      newData.dormDetail = data.aaAddress;
      newData.name = data.aaName;
      newData.mobile = data.aaMobile;
      newData.sex = data.aaSex;
      newData.customAddress = data.aaCustomAddress;
      newList.push(newData);
    });
    return newList;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return app.globalData.shareInfo;
  },

  /**
   * 选择地址
   */
  chooseAddress: function (event) {
    console.log(this.data.toDo);
    if (this.data.toDo == 'express'){
      wx.redirectTo({
        url: './../../take/take?aaId=' + event.currentTarget.id
      })
    } else if (this.data.toDo == 'takeOut'){
      wx.redirectTo({
        url: './../../errand/buy/buy?aaId=' + event.currentTarget.id
      })
    }
  },

  /**
   * 删除地址
   */
  delAddress: function (event) {
    var that = this;

    wx.showModal({
      title: '提示',
      content: '确定要删除这条信息吗？',
      success: function (res) {
        if (res.confirm) {
          var postData = {
            aaId: event.currentTarget.id,
            token: app.globalData.userInfo.token
          };
          util.myRequest(api.DelReceiveAddress, postData, function(data){
            that.loadList();
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  goAdd: function (event) {
    var that = this;
    wx.redirectTo({
      url: './../add/add?orderType=' + that.data.orderType + '&do=' + that.data.toDo
    })
  },
})